function Graph(obj) {
	this.obj = obj
	this.max = 0
	this.min = 0
	this.count = 0
	this.series = new Array()
	this.labels = new Array()
}

function addSeries(series) {
	this.series.push(series)
	this.count = this.countPoints()
	this.max = this.maxValue()
	this.min = this.minValue()
}
Graph.prototype.addSeries = addSeries

function maxValue(stacked) {
	var max = -9999999
	if (!stacked) {
		for (var s in this.series) {
			for (var ss in this.series[s]) {
				max = Math.max(max,this.series[s][ss])
			}
		}
		return max
	} else {
		for (var i = 0; i<this.count; i++) {
			var val = 0
			for (var s in this.series) {
				val += this.series[s][i]
			}
			max = Math.max(max,val)
		}
		return max
	}
}
Graph.prototype.maxValue = maxValue

function minValue(stacked) {
	var min = -9999999
	if (!stacked) {
		for (var s in this.series) {
			for (var ss in this.series[s]) {
				min = Math.min(min,this.series[s][ss])
			}
		}
		return min
	} else {
		for (var i = 0; i<this.count; i++) {
			var val = 0
			for (var s in this.series) {
				val += this.series[s][i]
			}
			min = Math.min(min,val)
		}
		return min
	}
}
Graph.prototype.minValue = minValue

function countPoints() {
	var count = 0
	for (var s in this.series) {
		count = Math.max(count,this.series[s].length)
	}
	return count
}
Graph.prototype.countPoints = countPoints

function drawBoxPlot() {
	if (this.series.length!=1) {
		var box = document.createElement("div")
		box.appendChild(document.createTextNode("Box Plot can only be used with ONE series"))
		this.obj.appendChild(box)
		return
	}
	this.count = this.countPoints()
	this.max = this.maxValue()
	this.min = this.minValue()
	
	var heightFactor = this.obj.offsetHeight / this.max
	var widthFactor = this.obj.offsetWidth / this.count
	var boxWidth = widthFactor * 0.8
	
	var offsetTop = this.obj.offsetTop
	var offsetLeft = this.obj.offsetLeft
	var offsetHeight = this.obj.offsetHeight
	
	for (var i = 0;i<this.count;i++) {
		for (var s in this.series) {
			var series = this.series[s]
			if (series.length>i) {
				var height = (series[i] * heightFactor)
				var box = document.createElement("div")
				box.style.position = "absolute"
				box.style.left = offsetLeft + (i * widthFactor) + "px"
				box.style.height = height + "px"
				box.style.width = boxWidth + "px"
				box.style.top = (offsetTop + offsetHeight - height) + "px"
				box.style.background = "blue"
				this.obj.appendChild(box)
			}
		}	
	}
}
Graph.prototype.drawBoxPlot = drawBoxPlot

function clearPlot() {
	while (this.obj.firstChild) {
		this.obj.removeChild(this.obj.firstChild)
	}
}
Graph.prototype.clearPlot = clearPlot

function clearSeries() {
	this.series = new Array()
}
Graph.prototype.clearSeries = clearSeries

function drawFloatingBoxPlot() {
	if (this.series.length!=2) {
		var box = document.createElement("div")
		box.appendChild(document.createTextNode("Box Plot can only be used with TWO serieses"))
		this.obj.appendChild(box)
		return
	}
	this.count = this.countPoints()
	this.max = this.maxValue(true)
	this.min = this.minValue(true)
	
	var heightFactor = this.obj.offsetHeight / this.max
	var widthFactor = this.obj.offsetWidth / this.count
	var boxWidth = widthFactor * 0.8
	
	var offsetTop = this.obj.offsetTop
	var offsetLeft = this.obj.offsetLeft
	var offsetHeight = this.obj.offsetHeight
	
	for (var i = 0;i<this.count;i++) {
		for (var s = 0;s==0;s++) {
			var series = this.series[s]
			if (series.length>i) {
				var height = (series[i] * heightFactor)
				var bottom = offsetHeight - height - (heightFactor * this.series[s+1][i])
				var box = document.createElement("div")
				box.style.position = "absolute"
				box.style.left = offsetLeft + (i * widthFactor) + "px"
				box.style.height = height + "px"
				box.style.width = boxWidth + "px"
				box.style.top = (offsetTop + bottom) + "px"
				box.style.background = "blue"
				this.obj.appendChild(box)
			}
		}	
	}
	
	if (this.showSeriesLabels)
		this.showSeriesLabels()
}
Graph.prototype.drawFloatingBoxPlot = drawFloatingBoxPlot

function showSeriesLabels() {
	var heightFactor = this.obj.offsetHeight / this.max
	var widthFactor = this.obj.offsetWidth / this.count
	var boxWidth = widthFactor
	
	var offsetTop = this.obj.offsetTop
	var offsetLeft = this.obj.offsetLeft
	var offsetHeight = this.obj.offsetHeight

	for (var l in this.labels) {
		var labelText = this.labels[l]
		var lbox = document.createElement("div")
		lbox.style.position = "absolute"
		lbox.style.fontSize = "10pt"
		lbox.style.textAlign = "center"
		lbox.style.left = offsetLeft + (l * widthFactor) + "px"
		lbox.style.height = "1.2em"
		lbox.style.width = boxWidth + "px"
		lbox.style.top = (offsetTop + offsetHeight) + "px"
		lbox.style.overflow = "hidden"
		lbox.appendChild(document.createTextNode(labelText))
		this.obj.appendChild(lbox)
	}
}
Graph.prototype.showSeriesLabels = showSeriesLabels

function addLabels(labels) {
	this.labels = labels
}
Graph.prototype.addLabels = addLabels


function graphTest() {
	var gb = document.createElement("div")
	gb.id = "graphBorder"
	var g = document.createElement("div")
	g.id = "graph"
	g = gb.appendChild(g)
	document.body.appendChild(gb)
	var g1 = new Graph(g)
	g1.addLabels(["A1","A2","A3","B1","B2","B3","C1","C2","C3"])
	g1.addSeries([1,2,3,4,5,6,7,8,4])
	//alert(s1)
	g1.drawBoxPlot()
	//var s2 = g1.addSeries([1,2,3,4,5,6,7,8,4])
	//var s3 = g1.addSeries([1,2,2,1,2,2,1,4,2])
	//g1.drawFloatingBoxPlot("blue",[s2,s3])
}